# Maintainer: Wu Zhenyu <wuzy01@qq.com>

_realname=bottom
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.10.2
pkgrel=1
pkgdesc="A graphical process/system monitor (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/ClementTsang/bottom"
msys2_documentation_url="https://clementtsang.github.io/bottom/${pkgver}"
msys2_references=(
  'purl: pkg:cargo/bottom'
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "tree"
             "git")
options=('!strip')
source=("git+${url}.git#tag=${pkgver}"
        "ntapi-rs.tar.gz::https://crates.io/api/v1/crates/ntapi/0.4.1/download"
        "ntapi-link-ntdll-arm64.patch")
sha256sums=('7e1691bff38827fa64d0df0e00425fea126416876291b18ef6160ad3e9b3bd14'
            'e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4'
            '9994d5b61f020f85f67df54c56c793bd4faf41e39b62ddd4e67c482b9a11b73e')

prepare() {
  cd "${_realname}"

  cat >> Cargo.toml <<END

[patch.crates-io]
ntapi = { path = "../ntapi-0.4.1" }
END

  patch -d ../ntapi-0.4.1 -i ../ntapi-link-ntdll-arm64.patch
  cargo update -p ntapi

  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}"

  export BTM_GENERATE=1
  export WINAPI_NO_BUNDLED_LIBRARIES=1
  cargo build --frozen --release --all-features
}

check() {
  cd "${_realname}"

  CARGO_HUSKY_DONT_INSTALL_HOOKS=true cargo test --frozen --release --all-features
}

package() {
  # binary
  install -vDm755 -t "${pkgdir}${MINGW_PREFIX}/bin" "${_realname}/target/release/btm"

  # license
  install -vDm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}" "${_realname}/LICENSE"

  # manpage and completions
  local _tmpdir="${_realname}/target/tmp/bottom"
  install -Dm644 "${_tmpdir}/manpage/btm.1" "${pkgdir}${MINGW_PREFIX}/share/man/man1/btm.1"
  install -Dm644 "${_tmpdir}/completion/_btm" "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_btm"
  install -Dm644 "${_tmpdir}/completion/btm.bash" "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/btm"
  install -Dm644 "${_tmpdir}/completion/btm.fish" "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/btm.fish"
}
